<template>
	<scroll-view :scroll-top="zjcScrollTop" scroll-y class="scroll-view" @scroll="handleScroll">
		<div style="margin: 0 auto;margin: 0.8em;">
			<!--查询-->
			<div style="margin-top: 2.0em;margin-bottom: 0.8em;font-size: 1.5em;">质保卡</div>
			<div style="display: flex;">
				<div style="width: calc(100% - 5em);">
					<input type="text" v-model="coNum" style="width: 100%;height: 2.0em;border: 1px solid #ccc;font-size: 0.9em;padding-left: 0.5em;box-sizing: border-box;" placeholder="按质保卡查询" />
				</div>
				<div class="serchbt"  @click="serch">查询
				</div>
			</div>
			<!--选项卡-->
			<div style="display: flex;margin-top: 2.5em;">
				<div :class="{'tab':true,'selectTab':selectIndex == 0}" @click="changeTab(0)">未使用</div>
				<div :class="{'tab':true,'selectTab':selectIndex == 1}" @click="changeTab(1)">已使用</div>
			</div>

			<div style="background-color: #A1A1A1;min-height: 60vh;padding: 0.5em;box-sizing: border-box;">
				<template v-if="selectIndex==0">
					<template v-for="(item,index) in objs" :key="index">
						<div
							style="margin: 0.2em;background-color: #ccc;padding: 0.5em;box-sizing: border-box;margin-bottom: 0.9em;">
							<div
								style="border-bottom: 1px solid #1f1f1f;padding-bottom: 0.2em;">
								<div>质保编号：{{item.coNum}}</div>
							</div>
							<div style="margin-top: 0.1em;display: flex;justify-content: space-between;">
								<div>创建时间：<uni-dateformat :date="item.creatTime" format="yyyy-MM-dd"></uni-dateformat></div>
								<div @click="toUse(item)">
									<div style="color: #fff;background-color: #007CFF;padding-bottom: 0.2em;padding-left: 0.6em;padding-right: 0.6em;font-size: 0.8em;margin-top: 0.2em;">去使用</div>
								</div>
							</div>
						</div>
					</template>
				</template>
				<template v-if="selectIndex==1">
					<template v-for="(item,index) in objs" :key="index">
						<div style="margin: 0.2em;background-color: #ccc;padding: 0.5em;box-sizing: border-box;margin-bottom: 0.9em;">
							<div style="border-bottom: 1px solid #1f1f1f;padding-bottom: 0.2em;">
								质保编号：{{item.coNum}}
							</div>
							<div style="margin-top: 0.1em;">
								创建时间：<uni-dateformat :date="item.creatTime" format="yyyy-MM-dd"></uni-dateformat>
							</div>
						</div>
					</template>
				</template>
			</div>
		</div>
	</scroll-view>
	<customTabbar ref="tabbar" />
</template>

<script setup>
	import {
		reactive,
		onMounted,
		ref
	} from 'vue';
	import customTabbar from '@/component/custom-tabbar.vue'
	import api from '@/common/httprequest.js'

	//上滑加载更多
	let objs = reactive([]);
	let pageNum = ref(1);
	let scrollThrottled = ref(false);
	let zjcScrollTop = ref(0);
	let toTopKey = ref(false);
	let lastScrollTop = ref(0);
	const winHeight = ref(0);
	const handleScroll = (e) => {
		const {
			scrollTop,
			scrollHeight
		} = e.detail;

		if (!scrollThrottled.value) {
			scrollThrottled.value = true;
			zjcScrollTop.value = e.detail.scrollTop;
			setTimeout(() => {
				scrollThrottled.value = false;
			}, 1500); // 调整延迟时间以适应需求
		}

		// console.log('scrollTop + winHeight.value',scrollTop + winHeight.value);
		// console.log('scrollHeight',scrollHeight);

		toTopKey.value = scrollTop > 100;
		if (scrollTop < lastScrollTop.value && lastScrollTop.value != 0) {
			return;
		}

		if (scrollTop + winHeight.value >= scrollHeight - 5) {
			lastScrollTop.value = scrollTop;
			console.log("上滑加载更多调用");
			onReachBottom(); // 达到底部时加载更多
		}
	};

	const onReachBottom = () => {
		pageNum.value++;
		loadData(false);
		//console.log("上滑加载更多调用a");
	}
	const toTop = () => {
		zjcScrollTop.value = 0;
	}
	//上滑加载更多结束	

	const loadData = (ifClear) => {
		if (ifClear) {
			objs.splice(0, objs.length);
		}
		uni.showLoading({
			title: '数据处理中...'
		});
		api.request({
			method: "Get",
			url: "/app/leagueNum/pageLeagueNum",
			params: {
				'pageNum': pageNum.value,
				'pageSize': 10,
				'status': selectIndex.value
			},
		}).then(res => {
			uni.hideLoading();
			console.log('resaa', res);
			if (res.code == 200) {
				var list = res.data;
				if (list.length == 0) {
					uni.showToast({
						title: '没有更多数据!',
						icon: null,
						duration: 2000
					})
					return;
				}
				objs.push(...list);
				console.log('objs', objs);
			} else {
				// uni.showToast({
				// 	icon: 'none',
				// 	duration: 2000,
				// 	title: '数据查询失败！'
				// })
			}

		});
	}


	let selectIndex = ref(0);
	const changeTab = (index) => {
		selectIndex.value = index;
		pageNum.value = 1;
		loadData(true);
	}


	let tabbar = ref(null);
	onMounted(() => {
		// 在非H5平台，可以通过uni.getSystemInfo获取屏幕信息
		uni.getSystemInfo({
			success: (info) => {
				winHeight.value = info.windowHeight;
			},
			fail: () => {
				console.error('获取系统信息失败');
			}
		});

		// 在H5平台，可以通过window.innerHeight获取
		if (typeof window !== 'undefined' && 'navigator' in window) {
			winHeight.value = window.innerHeight;
		}
		loadData(false);
		tabbar.value.activeIndex = 1;
	})


	const toUse = (item) => {
		uni.redirectTo({
			url: '/guarantee/add?coNum=' + item.coNum
		})
	}
	
	let coNum = ref(null);
	const serch = () => {
		if (coNum.value != null && coNum.value.length > 0) {
			serchData();
		} else {
			loadData(true);
		}
	}
	
	const serchData = () => {
		objs.splice(0, objs.length);
		uni.showLoading({
			title: '数据处理中...'
		});
		api.request({
			method: "Get",
			url: "/app/leagueNum/search",
			params: {
				'coNum': coNum.value,
				'joinNumStatus': selectIndex.value
			},
		}).then(res => {
			uni.hideLoading();
			console.log('resaa', res);
			if (res.code == 200) {
				var list = res.data;
				if (list.length == 0) {
					uni.showToast({
						title: '没有更多数据!',
						icon: null,
						duration: 2000
					})
					return;
				}
				objs.push(...list);
				console.log('objs', objs);
			} else {
				// uni.showToast({
				// 	icon: 'none',
				// 	duration: 2000,
				// 	title: '数据查询失败！'
				// })
			}
	
		});
	}
	
</script>

<style>
	page {
		background-color: #5E5E5E;
		color: #fff;
	}
</style>
<style scoped>
	.serchbt {
		width: 4em;
		background-color: #007CFF;
		color: #fff;
		border-radius: 0.2em;
		margin-left: 0.5em;
		text-align: center;
		height: 2.0em;
		line-height: 1.8em;
	}

	.tab {
		border-bottom: 2px solid #E3E3E3;
		flex: 1;
		text-align: center;
		padding: 0.3em;
		box-sizing: border-box;
		color: #fff;
	}

	.selectTab {
		border-bottom: 2px solid #007CFF;
		color: #007CFF;
	}
</style>